package com.teacher.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.auto.JSONAuthentication;
import com.teacher.entity.TClass;
import com.teacher.entity.TUser;
import com.teacher.entity.TXk;
import com.teacher.mapper.TClassMapper;
import com.teacher.mapper.TUserMapper;
import com.teacher.mapper.TXkMapper;
import com.teacher.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author teacher
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/t-xk")
public class TXkController extends JSONAuthentication {
    @Autowired
    TXkMapper tXkMapper;
    @Autowired
    TClassMapper tClassMapper;
    @Autowired
    TUserMapper tUserMapper;
    @ApiOperation(value = "添加学生选课记录")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("classId") int  classId, @RequestParam("stuId") int  stuId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("class_id",classId);
        wrapper.eq("stu_id",stuId);
        TXk tXk = tXkMapper.selectOne(wrapper);
        if(tXk!=null){
            Result error = Result.error().message("已选课，不要重复提交");
            this.WriteJSON(request, response, error);
        }else{
            if(tClassMapper.selectById(classId)==null){
                Result error = Result.error().message("无该课程");
                this.WriteJSON(request, response, error);
            }else{
                TClass tClass = tClassMapper.selectById(classId);
                if(tClass.getNownum()>=tClass.getTotalnum()){
                    Result error = Result.error().message("课程人数已满");
                    this.WriteJSON(request, response, error);
                }else{
                    TXk tXk1=new TXk();
                    tXk1.setClassId(classId);
                    tXk1.setStuId(stuId);
                    tClass.setNownum(tClass.getNownum()+1);
                    if(tXkMapper.insert(tXk1)>0&&tClassMapper.updateById(tClass)>0){
                        Result ok = Result.ok();
                        this.WriteJSON(request, response, ok);
                    }else{
                        Result error = Result.error();
                        this.WriteJSON(request, response, error);
                    }
                }
            }

        }
    }
    @ApiOperation(value = "删除学生选课记录")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        TXk tXk = tXkMapper.selectById(id);
        Integer classId = tXk.getClassId();
        TClass tClass = tClassMapper.selectById(classId);
        tClass.setNownum(tClass.getNownum()-1);
        if (tXkMapper.deleteById(id)>0&&tClassMapper.updateById(tClass)>0) {
            Result ok = Result.ok().message("退课成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }


    @ApiOperation(value = "修改当前学生课程的分数")
    @PostMapping("/updGrade")
    public void updGrade(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id, @RequestParam("grade") int grade,@RequestParam("pscj") int pscj) throws Exception {
        TXk tXk = tXkMapper.selectById(id);
        tXk.setPscj(pscj);
        tXk.setGrade(grade);
        if (tXkMapper.updateById(tXk)>0) {
            Result ok = Result.ok().message("修改完成");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找所有选课记录")
    @PostMapping("/findAllXk")
    public void findAllXk(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<TXk> shUserPage = new Page<>(current, size);
        Page<TXk> shUserPages = tXkMapper.selectPage(shUserPage, null);
        List<TXk> records = shUserPages.getRecords();
        int total = tXkMapper.selectCount(null);
        for (TXk a:records){
            TClass tClass = tClassMapper.selectById(a.getClassId());
            try {
                tClass.setTUserTeacher(tUserMapper.selectById(tClass.getStuId()));
                a.setTClass(tClass);
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                a.setTUserstudent(tUserMapper.selectById(a.getStuId()));
            }catch (Exception e){
                e.printStackTrace();
            }
            a.setZongfen((a.getGrade()*0.6+a.getPscj()*0.4));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xk", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找所有选课记录根据用户id")
    @PostMapping("/findAllXkById")
    public void findAllXk(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        Page<TXk> shUserPage = new Page<>(current, size);
        Page<TXk> shUserPages = tXkMapper.selectPage(shUserPage, wrapper);
        List<TXk> records = shUserPages.getRecords();
        int total = tXkMapper.selectCount(wrapper);
        for (TXk a:records){
            TClass tClass = tClassMapper.selectById(a.getClassId());
            tClass.setTUserTeacher(tUserMapper.selectById(tClass.getStuId()));
            a.setTClass(tClass);
            a.setTUserstudent(tUserMapper.selectById(a.getStuId()));
            a.setZongfen((a.getGrade()*0.6+a.getPscj()*0.4));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xk", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "老师查询自己所授课的选课人根据课程名和老师id")
    @PostMapping("/findXkByTeacher")
    public void findXkByTeacher(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")String userId,@RequestParam("className")String className) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        wrapper.eq("name",className);
        TClass tClass = tClassMapper.selectOne(wrapper);

        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("class_id",tClass.getId());

        Page<TXk> shUserPage = new Page<>(current, size);
        Page<TXk> shUserPages = tXkMapper.selectPage(shUserPage, wrapper1);
        List<TXk> records = shUserPages.getRecords();
        for (TXk a:records){
            TClass tClass1 = tClassMapper.selectById(a.getClassId());
            tClass1.setTUserTeacher(tUserMapper.selectById(tClass.getStuId()));
            a.setTClass(tClass1);
            a.setTUserstudent(tUserMapper.selectById(a.getStuId()));
            a.setZongfen((a.getGrade()*0.6+a.getPscj()*0.4));
        }
        int total = tXkMapper.selectCount(wrapper1);
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xk", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找所有选课记录根据用户id并且分数是大于等于60分")
    @PostMapping("/findAllXkByIdHeGe")
    public void findAllXkByIdHeGe(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        wrapper.gt("grade",60);
        Page<TXk> shUserPage = new Page<>(current, size);
        Page<TXk> shUserPages = tXkMapper.selectPage(shUserPage, wrapper);
        List<TXk> records = shUserPages.getRecords();
        int total = tXkMapper.selectCount(wrapper);
        for (TXk a:records){
            TClass tClass = tClassMapper.selectById(a.getClassId());
            tClass.setTUserTeacher(tUserMapper.selectById(tClass.getStuId()));
            a.setTClass(tClass);
            a.setTUserstudent(tUserMapper.selectById(a.getStuId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xk", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有选课记录根据用户id并且分数是小于60分")
    @PostMapping("/findAllXkByIdBuHeGe")
    public void findAllXkByIdBuHeGe(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        wrapper.lt("grade",60);
        Page<TXk> shUserPage = new Page<>(current, size);
        Page<TXk> shUserPages = tXkMapper.selectPage(shUserPage, wrapper);
        List<TXk> records = shUserPages.getRecords();
        int total = tXkMapper.selectCount(wrapper);
        for (TXk a:records){
            TClass tClass = tClassMapper.selectById(a.getClassId());
            tClass.setTUserTeacher(tUserMapper.selectById(tClass.getStuId()));
            a.setTClass(tClass);
            a.setTUserstudent(tUserMapper.selectById(a.getStuId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("Xk", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询自己选课的平均成绩")
    @PostMapping("/findAllXkAvg")
    public void findAllXkAvg(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        wrapper.ne("grade",0);
        List<TXk> list = tXkMapper.selectList(wrapper);
        int total=0;
        for (TXk a:list){
            if(a.getGrade()!=null){
                total+=a.getGrade();
            }

        }
        double avg=(total/list.size());
        if (list.size() > 0) {
            Result ok = Result.ok().data("avg", avg);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询自己选课的总成绩")
    @PostMapping("/findAllXkTotal")
    public void findAllXkTotal(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        List<TXk> list = tXkMapper.selectList(wrapper);
        int total=0;
        for (TXk a:list){
                total+=a.getGrade();

        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找用户未选的课程")
    @PostMapping("/findAllXkByIdWxk")
    public void findAllXkByIdWxk(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId")String userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("stu_id",userId);
        List<TXk> list = tXkMapper.selectList(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();
        for (TXk b:list){
            wrapper1.ne("id",b.getClassId());
        }
        Page<TClass> shUserPage = new Page<>(current, size);
        Page<TClass> shUserPages = tClassMapper.selectPage(shUserPage, wrapper1);
        List<TClass> records = shUserPages.getRecords();
        int total = tXkMapper.selectCount(wrapper1);
        for (TClass a:records){
            a.setTUserTeacher(tUserMapper.selectById(a.getStuId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("class", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找一门课都未选的学生")
    @PostMapping("/findAllXkByIdWxkNoOne")
    public void findAllXkByIdWxkNoOne(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        List<TXk> tXks = tXkMapper.selectList(null);
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("role","学生");
        for (TXk b:tXks){
            wrapper1.ne("id",b.getStuId());
        }
        Page<TUser> shUserPage = new Page<>(current, size);
        Page<TUser> shUserPages = tUserMapper.selectPage(shUserPage, wrapper1);
        List<TUser> records = shUserPages.getRecords();
        int total = tUserMapper.selectCount(wrapper1);
        if (records.size() > 0) {
            Result ok = Result.ok().data("class", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

